package com.hzqy.service.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.hzqy.commons.utils.*;
import com.hzqy.web.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.persistence.dao.cast.CastDao;
import com.hzqy.persistence.dao.picture.PictureDao;
import com.hzqy.persistence.dao.pms.PmsDao;
import com.hzqy.persistence.dao.user.UserDao;
import com.hzqy.service.pms.PmsServiceBean;

@Service
public class UserServiceBean {
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());

	@Resource
	private UserDao userDao;
	@Resource
	private PmsDao pmsDao;
	@Resource
	private PmsServiceBean pmsServiceBean;

	
	public int addUserOrgRoles(PmsUserVo pmsUserVo) {
		int f_po_id = pmsUserVo.getOrgId();
		int flag = 0;
		flag = pmsDao.insertUser(pmsUserVo);
		int f_pu_id = pmsUserVo.getF_pu_id();
		LOG.debug("用户增加flag："+flag);
		LOG.debug("用户id："+f_pu_id);
		
		PmsOrgUserVo pouv = new PmsOrgUserVo();
		pouv.setF_pou_userId(f_pu_id);
		pouv.setF_pou_orgId(f_po_id);
		flag = pmsDao.insertOrgUser(pouv);
		LOG.debug("组织用户增加flag："+flag);
		
		if(pmsUserVo.getRoleIds().indexOf(",")!=-1){
			String[] roleIds = pmsUserVo.getRoleIds().split(",");
			if(roleIds.length>0) {
				PmsOrgRoleVo porv = new PmsOrgRoleVo();
				for(int i=0;i<roleIds.length;i++) {
					String roleId = roleIds[i];
					porv.setF_por_orgId(f_po_id);
					porv.setF_por_roleId(Integer.valueOf(roleId));
					List<PmsOrgRoleVo> porVoList = userDao.selectOrgRole(porv);
					if(porVoList.isEmpty()){
						flag = pmsDao.insertOrgRole(porv);
						LOG.debug("组织角色增加flag："+flag);
					}else{
						LOG.debug("已存在");
					}
					
				}
			}
		}
		if(pmsUserVo.getF_pour_roleIds().indexOf(",")!=-1){
			String[] f_pour_roleIds = pmsUserVo.getF_pour_roleIds().split(",");
			if(f_pour_roleIds.length>0) {
				for(int i=0;i<f_pour_roleIds.length;i++) {
					PmsOrgUserRoleVo pourv = new PmsOrgUserRoleVo();
					String roleId = f_pour_roleIds[i];
					pourv.setF_pour_orgId(f_po_id);
					pourv.setF_pour_userId(f_pu_id);
					pourv.setF_pour_roleId(Integer.valueOf(roleId));
					flag = pmsDao.insertOrgUserRole(pourv);
					LOG.debug("组织角色用户增加flag："+flag);
				}
			}
		}
		return flag;
	}
	
	public Map<String,Object> selectUserList(HttpSession session,PmsUserVo puv, int curPage,int pageSize) {
		Map<String, Object> map =new HashMap<String, Object>();
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		String orgIds = pmsServiceBean.getAuthorityPmsOrgIds(userVo);
		puv.setOrgIds(orgIds);
		int rowCount  = userDao.findUserCount(puv);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0){
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_pu_name", puv.getF_pu_name());
			map.put("f_pu_realName", puv.getF_pu_realName());
			map.put("orgId", puv.getOrgId());
			map.put("orgIds", puv.getOrgIds());
			List<PmsUserVo> results = new ArrayList<PmsUserVo>();
			results = userDao.findUserList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<PmsUserVo>());
		}
		
		return map;
	}
	
	public int findUserIsExist(PmsUserVo puv) {
		return pmsServiceBean.findUserIsExist(puv);
	}
	
	public int updateUser(PmsUserVo puv) {
		int flag =pmsServiceBean.updateUser(puv);
		
		flag = userDao.deleteRoleByUserId(puv.getF_pu_id());
		int f_po_id = puv.getOrgId();
		int f_pu_id = puv.getF_pu_id();
		if(puv.getF_pour_roleIds().indexOf(",")!=-1){
			String[] f_pour_roleIds = puv.getF_pour_roleIds().split(",");
			if(f_pour_roleIds.length>0) {
				for(int i=0;i<f_pour_roleIds.length;i++) {
					PmsOrgUserRoleVo pourv = new PmsOrgUserRoleVo();
					String roleId = f_pour_roleIds[i];
					pourv.setF_pour_orgId(f_po_id);
					pourv.setF_pour_userId(f_pu_id);
					pourv.setF_pour_roleId(Integer.valueOf(roleId));
					flag = pmsDao.insertOrgUserRole(pourv);
					LOG.debug("组织角色用户增加flag："+flag);
				}
			}
		}else if(!"".equals(puv.getF_pour_roleIds().trim())){
			PmsOrgUserRoleVo pourv = new PmsOrgUserRoleVo();
			pourv.setF_pour_orgId(f_po_id);
			pourv.setF_pour_userId(f_pu_id);
			pourv.setF_pour_roleId(Integer.valueOf(puv.getF_pour_roleIds()));
			flag = pmsDao.insertOrgUserRole(pourv);
			LOG.debug("组织角色用户增加flag："+flag);
		}
		return flag;
	}
	
	public List<PmsOrgRoleVo> selectOrgRole(PmsUserVo userVo) {
		List<PmsOrgRoleVo> porvList = new ArrayList<PmsOrgRoleVo>();
		if(userVo.getParentOrgId()==0){
			porvList = userDao.findAllRoleList();
		}else{
			porvList = pmsDao.selectOrgRole(userVo.getOrgId());
		}
		
		return porvList;
	}
	
	public PmsUserVo findUserById(HttpSession session,PmsUserVo puv) {
		PmsUserVo userVo = (PmsUserVo) session.getAttribute(ConstantUtils.SESSION_USER);
		String orgIds = pmsServiceBean.getAuthorityPmsOrgIds(userVo);
		puv.setOrgIds(orgIds);
		PmsUserVo pmsUserVo= userDao.findUserById(puv);//如果是空 说明没权限查
		if(pmsUserVo!=null){
			PmsUserVo pp = new PmsUserVo();
			pp = pmsDao.findUserById(puv.getF_pu_id());
			PmsRoleVo prv = userDao.findRoleById(puv.getF_pu_id());
			if(prv==null){
				pp.setRoleNames("无");
				pp.setRoleIds("");
			}else{
				pp.setRoleNames(prv.getRoleNames());
				pp.setRoleIds(prv.getRoleIds());
			}
			return pp;
		}
		return null;
	}

	public Map<String,Object> findUser(HttpServletRequest request, PmsUserVo puv) {
		//查询用户信息 若为空，说明登录有问题
		PmsUserVo pmsUserVo= userDao.findUser(puv);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		if(pmsUserVo!=null){
			resultMap.put("success", "true");
			resultMap.put("phone", pmsUserVo.getF_pu_phone());
		}else{
			resultMap.put("success", "false");
			resultMap.put("result", "用户名或密码错误！");
		}
		return resultMap;
	}

}
